package wwy.Service;

import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import wwy.entity.Foods;
import wwy.entity.Merchant;

import java.util.Arrays;
import java.util.List;

@Service
public class Init_Merchant_Foods {
    private final MerchantService merchantService;
    private final FoodsService foodService;

    public Init_Merchant_Foods(MerchantService merchantService, FoodsService foodService) {
        this.merchantService = merchantService;
        this.foodService = foodService;
    }
    // 初始化商家和食品数据
    @Transactional
    @EventListener(ApplicationReadyEvent.class)
    public void init() {
        if( merchantService.getAllMerchants().isEmpty() && foodService.getAll().isEmpty()){
            List<Merchant> merchants = Arrays.asList(
                createMerchant("万家饺子（软件园E18店）", "沈阳浑南区软件园E18楼",
                        "各种饺子",15.0, 3.0),
                createMerchant("小锅饭豆腐馆（全运店）", "沈阳市全运路126号",
                        "小锅套餐", 16.0, 2.5),
                createMerchant("麦当劳麦乐送（全运路店）", "沈阳市全运路53号麦当劳",
                        "汉堡薯条",20.0, 4.0),
                createMerchant("米村拌饭（浑南店）", "沈阳市浑南区彩霞街26号",
                        "拌饭套餐",15.0, 3.0),
                createMerchant("申记串道（中海康城店）", "沈阳市中海康城社区48号",
                        "烤串炸串", 18.0, 3.0),
                createMerchant("半亩良田排骨米饭", "沈阳市和平区文萃路58号",
                        "排骨米饭", 15.0, 3.0),
                createMerchant("茶兮鲜果饮品（国际软件园店）", "沈阳市国际软件园6号楼",
                        "甜品饮品",20.0, 2.0),
                createMerchant("唯一水果捞（软件园E18店）", "沈阳市软件园E18楼",
                        "新鲜水果",15.0, 5.0),
                createMerchant("满园春饼（全运路店）", "沈阳市全运路99号",
                        "春饼炒菜",15.0, 1.0)
            );
            merchantService.saveMerchants(merchants);
            List<Foods> foodsList = Arrays.asList(
                    createFoods("纯肉鲜肉（水饺）", "纯肉馅饺子",
                            16.00, merchants.getFirst()),
                    createFoods("玉米鲜肉（水饺）", "玉米清香",
                            15.00, merchants.getFirst()),
                    createFoods("虾仁三鲜（蒸饺）", "三鲜馅饺子",
                            20.00, merchants.getFirst()),
                    createFoods("素三鲜（蒸饺）", "韭菜鸡蛋虾仁",
                            15.00, merchants.getFirst()),
                    createFoods("角瓜鸡蛋（蒸饺）", "角瓜清香",
                            15.00, merchants.getFirst()),
                    createFoods("小白菜肉（水饺）", "小白菜鲜肉",
                            16.00, merchants.getFirst()),
                    createFoods("芹菜牛肉（水饺）", "芹菜牛肉",
                           22.00, merchants.getFirst()),
                    createFoods("虾腰鲜肉（蒸饺）", "虾仁腰子",
                           25.00, merchants.getFirst()),
                    createFoods("青椒鲜肉（蒸饺）", "青椒鲜肉",
                            18.00, merchants.getFirst()),
                    createFoods("蛋黄焗豆花", "蛋黄豆花",
                            8.00, merchants.get(1)),
                    createFoods("麻婆豆腐", "麻辣味",
                           14.00, merchants.get(1)),
                    createFoods("广式小豆腐", "广东风味",
                            12.00, merchants.get(1))
                    );
            foodService.saveFoods(foodsList);
        }
    }

    private Merchant createMerchant(String name, String address, String description,
                                    Double deliveryPrice, Double startDeliveryPrice) {
        Merchant merchant = new Merchant();
        merchant.setName(name);
        merchant.setAddress(address);
        merchant.setDescription(description);
        merchant.setDeliveryPrice(deliveryPrice);
        merchant.setStartDeliveryPrice(startDeliveryPrice);
        return merchant;
    }
    private Foods createFoods(String name, String description,
                              Double price, Merchant merchant) {
        Foods foods = new Foods();
        foods.setName(name);
        foods.setDescription(description);
        foods.setPrice(price);
        foods.setMerchant(merchant);
        return foods;
    }
}
